package com.gouwudai.gobbs.service.impl;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.extremecomponents.table.limit.Limit;

import com.gouwudai.gobbs.common.Constants;
import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IMasterDAO;
import com.gouwudai.gobbs.pojo.TbbsMaster;
import com.gouwudai.gobbs.pojo.TbbsMasterPermission;
import com.gouwudai.gobbs.service.IMasterService;
import com.gouwudai.gobbs.vo.MasterLoginVO;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.exception.DBException;
import com.litt.core.exception.NotLoginException;
import com.litt.core.util.ExtremeTableUtil;
import com.litt.core.util.ParamUtils;

public class MasterServiceImpl implements IMasterService 
{
	IMasterDAO masterDAO;		
	
	public void save(TbbsMaster tbbsMaster) throws BusiException
	{ 
        masterDAO.save(tbbsMaster);
	}

	public TbbsMaster load(Integer id)
	{
		return masterDAO.load(id);
	}

	public void update(TbbsMaster tbbsMaster) throws BusiException
	{
			masterDAO.update(tbbsMaster);
	}
	
	public void updatePassword(String password,Integer masterId)
	{
			masterDAO.updatePassword(password, masterId);
	}	

	public void delete(Integer masterId)
	{
			masterDAO.delete(masterId);
	}


	public IPageList listPage(PageParam pageParam)
	{
		return masterDAO.listPage(pageParam);
	}
	
	public void AutoBindingTable(javax.servlet.http.HttpServletRequest request) throws DBException
	{
		Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
		IPageList page = this.listPage(new PageParam(limit.getPage(), limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit)));
		request.setAttribute("resultList",page.getRsList());
		request.setAttribute("totalRows",page.getTotalSize());
	}	
	
	public void AutoBinding(HttpServletRequest request,TbbsMaster tbbsMaster)
	{
		tbbsMaster = new TbbsMaster();		
		ParamUtils.DynaBinder(tbbsMaster,request);	//动态绑定request值到对象中		
	}	
	
	
	public MasterLoginVO masterLogin(String loginId,String password,String loginIp) throws BusiException
	{
		TbbsMaster tbbsMaster = (TbbsMaster)masterDAO.load(loginId);
		//判断登陆名
		if(tbbsMaster==null)
			throw new NotLoginException("登陆名不存在");			
		String md5Pass = Utility.MD5EncodeString(password);		
		//判断密码
		if(!tbbsMaster.getPassword().equals(md5Pass))
			throw new NotLoginException("密码错误");
		//判断是否锁定IP
		if(tbbsMaster.getIpLockFlag()&&!tbbsMaster.getLastLoginIp().equals(loginIp))
			throw new NotLoginException("非指定IP登陆");
		//更新管理员登陆相关信息
		masterDAO.doLoginUpdate(tbbsMaster.getMasterId(),loginIp);
		//将对象属性设到VO中，并存入SESSION
		MasterLoginVO vo = new MasterLoginVO();
		ParamUtils.DynaBinder(vo,tbbsMaster);
		//查询登陆管理员的权限		
		List list = masterDAO.getMgPermission(tbbsMaster.getMgId());
		
		TbbsMasterPermission tbbsMasterPermission = null;		
		for(int i=0;i<list.size();i++)
		{			
			tbbsMasterPermission = (TbbsMasterPermission)list.get(i);			
			vo.addPermission(tbbsMasterPermission.getPermissionCode(), tbbsMasterPermission.getPermissionName());
		}
		return vo;
	}	
	
	/**
	 * 判断Cookie中是否保存用户名密码
	 * @param cookies
	 * @param loginIp
	 * @throws DBException
	 * @throws BusiException
	 */
	public MasterLoginVO masterLogin(Cookie[] cookies,String loginIp) throws BusiException
	{
		String loginId = "";
		String password = "";
		
		if (cookies!=null)
		{
			for (int i = 0; i < cookies.length; i++)
			{
				Cookie c = cookies[i];
				String name = c.getName().trim();
				String value = c.getValue().trim();
				
				if(name.equals("M_LOGIN_ID"))
				{
					loginId = value;
				}
				if(name.equals("M_PASSWORD")){
					password = value;
				}		
						
				
			}
		}
		if(loginId.equals("")||password.equals(""))
			throw new NotLoginException("");
		return masterLogin(loginId,password,loginIp);
	}

	public void setMasterDAO(IMasterDAO masterDAO) {
		this.masterDAO = masterDAO;
	}	

}